package com.bls.sms.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bls.sms.bo.interfaces.SubjectBO;
import com.bls.sms.exception.ApplicationException;
import com.bls.sms.struts.form.SubjectForm;
import com.bls.sms.wrapper.Subject;

public class SubjectAction extends BaseAction {
	private SubjectBO subjectBO = null;

	public ActionForward addSubject(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			SubjectForm form = (SubjectForm) actionForm;
			Subject subject = new Subject();
			setWrapperValues(form, subject);
			getSubjectBO().saveSubject(subject);
		} catch (ApplicationException e) {
			e.printStackTrace();
		}
		return actionMapping.findForward("success");
	}

	public ActionForward updateSubject(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			SubjectForm form = (SubjectForm) actionForm;
			Subject subject = null;
			setWrapperValues(form, subject);
			getSubjectBO().updateSubject(subject);
		} catch (ApplicationException e) {
			e.printStackTrace();
		}
		return actionMapping.findForward("success");
	}

	public ActionForward deleteSubject(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			SubjectForm form = (SubjectForm) actionForm;
			getSubjectBO().deleteSubject(form.getId());
		} catch (ApplicationException e) {
			e.printStackTrace();
		}
		return actionMapping.findForward("success");
	}

	public ActionForward listSubjects(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		List<Subject> list = null;
		try {
			list = getSubjectBO().loadSubjects();
		} catch (ApplicationException e) {
			e.printStackTrace();
		}
		request.setAttribute("list", list);
		return actionMapping.findForward("view_subjects");
	}

	public ActionForward getSubjectDetails(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			SubjectForm form = (SubjectForm) actionForm;
			Subject subject = getSubjectBO().getSubjectDetails(form.getId());
			setFormValues(form, subject);
			request.setAttribute("SubjectDetails", form);
		} catch (ApplicationException e) {
			e.printStackTrace();
		}
		return actionMapping.findForward("view_subject_details");
	}

	private void setWrapperValues(SubjectForm form, Subject subject) {
		subject.setId(form.getId());
		subject.setName(form.getName());
	}

	private void setFormValues(SubjectForm form, Subject subject) {
		if (subject == null) {
			subject = new Subject();
		}
		form.setId(subject.getId());
		form.setName(subject.getName());
	}

	public SubjectBO getSubjectBO() {
		return subjectBO;
	}

	public void setSubjectBO(SubjectBO subjectBO) {
		this.subjectBO = subjectBO;
	}
}
